package cn.template.serve_project.controller;


import cn.template.serve_project.aop.note.Authentication;
import cn.template.serve_project.aop.note.Encrypt;
import cn.template.serve_project.dto.admin.AdminAddDTO;
import cn.template.serve_project.dto.admin.AdminPageDTO;
import cn.template.serve_project.dto.admin.AdminUpdateDTO;
import cn.template.serve_project.dto.excel.ExcelAdminDTO;
import cn.template.serve_project.enums.RoleEnum;
import cn.template.serve_project.result.Result;
import cn.template.serve_project.service.AdminService;
import cn.template.serve_project.vo.admin.AdminPageVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 管理员(Admin)表控制层
 */
@RestController
@RequestMapping("/admin")
@Valid
public class AdminController {
    /**
     * 服务对象
     */
    @Resource
    private AdminService adminService;


    /**
     * 新增用户管理员(Admin)
     *
     * @param dto 参数对象
     */
    @Authentication(role = RoleEnum.SUPER_ADMIN)
    @PostMapping("/auth/add")
    public Result<?> add(@Valid @RequestBody AdminAddDTO dto) {
        adminService.addAdmin(dto);
        return Result.success();
    }


    /**
     * 分页查询管理员信息(Admin)
     *
     * @param dto 参数对象
     * @return 数据列表
     */
    @Authentication(role = RoleEnum.ADMIN)
    @GetMapping("/auth/page")
    public Result<AdminPageVO> page(@Valid @ModelAttribute AdminPageDTO dto) {
        return Result.success(adminService.selectPage(dto));
    }


    /**
     * 导出excel文件
     *
     * @param excelObj 参数对象
     * @param response 请求对象
     */
    @Authentication(role = RoleEnum.SUPER_ADMIN)
    @PostMapping("/auth/export/excel")
    public void exportExcel(@Valid @RequestBody ExcelAdminDTO excelObj, HttpServletResponse response) throws IOException {
        adminService.export(excelObj, response);
    }


    /**
     * 根据id列表删除(Admin)
     *
     * @param idList id列表
     * @return 成功数
     */
    @Authentication(role = RoleEnum.SUPER_ADMIN)
    @PostMapping("/auth/del/id")
    public Result<Integer> deleteInIdList(@Valid @RequestBody List<Long> idList) {
        return Result.success(adminService.deleteInIdList(idList));
    }


    /**
     * 变更管理员信息(Admin)
     *
     * @param dto 参数对象
     * @return 成功数
     */
    @Encrypt
    @PostMapping("/auth/update")
    public Result<?> updateAdmin(@Valid @RequestBody AdminUpdateDTO dto) {
        adminService.updateAdmin(dto);
        return Result.success();
    }


}

